import {debug, host} from "./config"

export default function(url, type = "GET", params) {
    if (type == "POST" || type == "PUT")
        params = JSON.stringify(params || {})

    let deferred = $.Deferred()
    let setup = {
        debug: debug,
        type: type,
        url: host + url,
        data: params,
        contentType: "application/json; charset=UTF-8",
        dataType: 'json',
        success: deferred.resolve,
        error: deferred.reject
    }

    if (localStorage['swallow_token']) {
        Object.assign(setup, {
            headers: {
                "Authorization": "Token " + localStorage['swallow_token']
            }
        })
    }

    $.ajax(setup)
    return deferred.promise()
}
